﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Text;
using ReflectionStudio.Core.Events;
namespace WPFExcelReport
{

    public class AB45MSUMData : BaseFunction
    {
        public bool flagRegenerateLastMonthData;
        ArrayList ListTris = new ArrayList();

        List<WGSummary> ListWGSummary;
        string TrisInclude = "";
        public int ThreadCount;
        public AB45MSUMData()
            : base()
        {
            
        }
        public AB45MSUMData(Dictionary<string, object> DicGlobalPara)
            : base(DicGlobalPara)
        {
        }
         
        public DataSet GetRawData(DataRow dwABPOS)
        {
            DataSet set4Return = new DataSet();
            DataSet setLocal = new DataSet();
            DataSet setResult = new DataSet();
            DataSet setTemp = new DataSet();
            DataTable tableLocal = null;
            string VKPAUFNUM;
            string WBSRTDATE, WBENTDATE;
            WBSRTDATE = dwABPOS["WBSRTDATE"].ToString().Trim();
            WBENTDATE = dwABPOS["WBENTDATE"].ToString().Trim();
            try
            {
                OracleCommand or1 = OracleDB.CreateCommand();
                #region
                
                or1.CommandText = "SELECT DISTINCT ' ' AS SUMYEAR,' ' AS SUMMONTH, LSLIEFNR,LSNAME1 AS FACTORY,ABPPOSTYP,COUNT(DISTINCT BMBEM) AS BATCHES "
                + " FROM ABPOS P LEFT JOIN ABFOLGE F ON ABFAUFKRS=ABPAUFKRS AND ABFAUFLFD=ABPAUFLFD AND ABFAUFPOS=ABPAUFPOS AND ABFKENNUNG='VW'"
                + " LEFT JOIN LIEFDAT L ON  F.ABFLIEFNR=L.LSLIEFNR"
                + " JOIN BABEW W ON BBAUFKRS=ABPAUFKRS AND BBAUFLFD=ABPAUFLFD AND BBAUFPOS=ABPAUFPOS"
                + " JOIN BABST B ON BMAUFKRS=BBAUFKRS AND BMAUFLFD=BBAUFLFD AND BMAUFPOS=BBAUFPOS"
                + " WHERE ABPAUFART='VW' AND ABPERLKZ='J' AND ABPERLDAT>='" + WBSRTDATE + "' AND ABPERLDAT<='" + WBENTDATE + "'"
                + " GROUP BY LSLIEFNR,LSNAME1,ABPPOSTYP"
                    + " ORDER BY 1,2,3,4";
                #endregion
                setLocal = Connect2Proxy(or1);
                if (setLocal.Tables.Count > 0)
                {
                    tableLocal = setLocal.Tables[0];
                    tableLocal.TableName = "AB45ABPOS";
                    SetDefault(tableLocal);
                    byte[] b;
                    string value;
                    string sTemp = "";
                    string Column = "";
                    string ARTICLE = "", WBBDPART, FACTORY;
                    int PCNUM;
                    double WBEWMTR;
                    List<DataRow> dltRows = new List<DataRow>();
                    ListWGSummary = new List<WGSummary>();
                    string SUMMONTH, SUMYEAR;
                    SUMYEAR = WBSRTDATE.Substring(0, 4);
                    SUMMONTH = WBSRTDATE.Substring(4, 2);
                    foreach (DataRow dwTemp in tableLocal.Rows)
                    {
                        dwTemp["SUMYEAR"] = SUMYEAR;
                        dwTemp["SUMMONTH"] = SUMMONTH;
                        Column = "FACTORY";
                        #region
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[Column].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp[Column] = value;
                        #endregion 
                    }

                    setLocal.AcceptChanges();
                    set4Return.Merge(setLocal);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return set4Return;
        }
        public DataSet BuildWBBST(DataRow dwUI)
        {
            string progress = "查询完成";
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            try
            {
                EventDispatcher.Instance.RaiseStatus("查询开始", StatusEventType.StartProgress);
                setResult = GetRawData(dwUI);
                setReturn.Merge(setResult);
                EventDispatcher.Instance.RaiseStatus("查询完成", StatusEventType.StopProgress);
            }
            catch (Exception ex)
            {
                Tracer.Error(ABPOS + ex.StackTrace, ex);
            }
            return setReturn;
        }
    }
}